﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_PRODUCT.Product.WTS
{
    public partial class frm_LaborWTSFind : DevExpress.XtraEditors.XtraForm
    {
        ClsWTSDAO clsWTSDAO = new ClsWTSDAO();
        ClsMachineWTSTable b = new ClsMachineWTSTable();
        DataTable dtStaff = new DataTable();
        DataTable dtMOno = new DataTable();
        private ctrlLaborWTSDetail ctrl;
        private bool flagLoadDone;
        private bool hasAnd;
        private bool dropDown;
        string Where = "where";
        public frm_LaborWTSFind(ctrlLaborWTSDetail ctrlMWTS)
        {
            InitializeComponent();
            this.ctrl = ctrlMWTS;
            this.ActiveControl = cbStaff;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            string sql = string.Empty;

            if (dateDateFrom.EditValue != null && dateDateTo.EditValue == null)
            {
                Where = Where + " convert(Date,[242_LaborWTS].date,111) = '" + dateDateFrom.DateTime.ToString("yyyy/MM/dd") + "' ";
                hasAnd = true;
            }
            else if (dateDateFrom.EditValue != null && dateDateTo.EditValue != null)
            {
                Where = Where + " convert(Date,[242_LaborWTS].date,111) >= '" + dateDateFrom.DateTime.ToString("yyyy/MM/dd") + "' and  convert(Date,[242_LaborWTS].date,111) <= '" + dateDateTo.DateTime.ToString("yyyy/MM/dd") + "'";
                hasAnd = true;
            }

            if (cbMachine.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].MachineID = '" + cbMachine.SelectedValue.ToString() + "'";

                }
                else
                {
                    Where = Where + " [242_LaborWTS].MachineID = '" + cbMachine.SelectedValue.ToString() + "'";
                    hasAnd = true;
                }
            }

            if (cbMONo.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].MONo = '" + cbMONo.Text.Trim().ToString() + "'";
                }
                else
                {
                    Where = Where + " [242_LaborWTS].MONo = '" + cbMONo.Text.Trim().ToString() + "'";
                    hasAnd = true;
                }
            }

            if (cbOption.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].OptionID = '" + cbOption.Text.Trim().ToString() + "'";
                }
                else
                {
                    Where = Where + " [242_LaborWTS].OptionID = '" + cbOption.Text.Trim().ToString() + "'";

                    hasAnd = true;
                }

            }

            if (cbStaff.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].StaffID = '" + cbStaff.SelectedValue.ToString() + "'";
                }
                else
                {
                    Where = Where + "  [242_LaborWTS].StaffID = '" + cbStaff.SelectedValue.ToString() + "'";
                    hasAnd = true;
                }
            }

            if (cbWorkID.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].WorkID = '" + cbWorkID.SelectedValue.ToString() + "'";
                }
                else
                {
                    Where = Where + "  [242_LaborWTS].WorkID = '" + cbWorkID.SelectedValue.ToString() + "'";
                    hasAnd = true;
                }
            }

            if (cbPartID.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MO].PartID like '%" + cbPartID.Text.Trim() + "%'";
                }
                else
                {
                    Where = Where + "  [242_MO].PartID like '%" + cbPartID.Text.Trim() + "%'";
                    hasAnd = true;
                }
            }

            if (cbShift.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].Shift = '" + cbShift.SelectedItem + "'";
                }
                else
                {
                    Where = Where + " [242_LaborWTS].Shift = '" + cbShift.SelectedItem + "'";
                    hasAnd = true;
                }
            }

            if (txtTime.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].Time = '" + txtTime.Text.Trim() + "'";
                }
                else
                {
                    Where = Where + " [242_LaborWTS].Time = '" + txtTime.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtOKQty.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    if (txtOKQty.Text.Trim() == "0")
                    {
                        Where = Where + " and ([242_LaborWTS].OKQty = '0' or (ISNULL([242_LaborWTS].OKQty, '') = ''))";
                    }
                    else
                    {
                        Where = Where + " and [242_LaborWTS].OKQty = '" + txtOKQty.Text.Trim() + "'";
                    }
                    //Where = Where + " and [242_LaborWTS].OKQty = '" + txtOKQty.Text.Trim() + "'";
                }
                else
                {
                    if (txtOKQty.Text.Trim() == "0")
                    {
                        Where = Where + "  ([242_LaborWTS].OKQty = '0' or (ISNULL([242_LaborWTS].OKQty, '') = ''))";
                    }
                    else
                    {
                        Where = Where + "  [242_LaborWTS].OKQty = '" + txtOKQty.Text.Trim() + "'";
                    }
                    //Where = Where + " [242_LaborWTS].OKQty = '" + txtOKQty.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtNGQty.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    if (txtNGQty.Text.Trim() == "0")
                    {
                        Where = Where + " and ([242_LaborWTS].NGQty = '0' or (ISNULL([242_LaborWTS].NGQty, '') = ''))";
                    }
                    else
                    {
                        Where = Where + " and [242_LaborWTS].NGQty = '" + txtNGQty.Text.Trim() + "'";
                    }
                    //Where = Where + " and [242_LaborWTS].NGQty = '" + txtNGQty.Text.Trim() + "'";
                }
                else
                {
                    if (txtNGQty.Text.Trim() == "0")
                    {
                        Where = Where + "  ([242_LaborWTS].NGQty = '0' or (ISNULL([242_LaborWTS].NGQty, '') = ''))";
                    }
                    else
                    {
                        Where = Where + "  [242_LaborWTS].NGQty = '" + txtNGQty.Text.Trim() + "'";
                    }
                    //Where = Where + " [242_LaborWTS].NGQty = '" + txtNGQty.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtProTime.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    if (txtProTime.Text.Trim() == "0")
                    {
                        Where = Where + " and ([242_LaborWTS].ProTime = '0' or (ISNULL([242_LaborWTS].ProTime, '') = ''))";
                    }
                    else
                    {
                        Where = Where + " and [242_LaborWTS].ProTime = '" + txtProTime.Text.Trim() + "'";
                    }
                }
                else
                {
                    if (txtProTime.Text.Trim() == "0")
                    {
                        Where = Where + "  ([242_LaborWTS].ProTime = '0' or (ISNULL([242_LaborWTS].ProTime, '') = ''))";
                    }
                    else
                    {
                        Where = Where + "  [242_LaborWTS].ProTime = '" + txtProTime.Text.Trim() + "'";
                    }

                    //Where = Where + " [242_LaborWTS].ProTime = '" + txtProTime.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtClampTime.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    if (txtClampTime.Text.Trim() == "0")
                    {
                        Where = Where + " and ([242_LaborWTS].ClampTime = '0' or (ISNULL([242_LaborWTS].ClampTime, '') = ''))";
                    }
                    else
                    {
                        Where = Where + " and [242_LaborWTS].ClampTime = '" + txtClampTime.Text.Trim() + "'";
                    }

                    //  Where = Where + " and [242_LaborWTS].ClampTime = '" + txtClampTime.Text.Trim() + "'";
                }
                else
                {
                    if (txtClampTime.Text.Trim() == "0")
                    {
                        Where = Where + "  ([242_LaborWTS].ClampTime = '0' or (ISNULL([242_LaborWTS].ClampTime, '') = ''))";
                    }
                    else
                    {
                        Where = Where + "  [242_LaborWTS].ClampTime = '" + txtClampTime.Text.Trim() + "'";
                    }

                    //Where = Where + " [242_LaborWTS].ClampTime = '" + txtClampTime.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtNote1.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].Note1 like '%" + txtNote1.Text.Trim() + "%'";
                }
                else
                {
                    Where = Where + " [242_LaborWTS].Note1 like '%" + txtNote1.Text.Trim() + "%'";
                    hasAnd = true;
                }
            }

            if (txtNote2.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_LaborWTS].Note2 = '" + txtNote2.Text.Trim() + "'";
                }
                else
                {
                    Where = Where + " [242_LaborWTS].Note2 = '" + txtNote2.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            sql = "Select [242_LaborWTS].*, [242_MO].PartID from [242_LaborWTS] " +
                    " RIGHT OUTER JOIN " +
                    "      [242_MO] ON [242_MO].MONo = [242_LaborWTS].MONo " +
               Where +
               " order by [242_LaborWTS].[date] desc";
            if (Where.Equals("where"))
            {
                MessageBox.Show("Hãy chọn ít nhất một điều kiện tìm kiếm.");

            }
            else
            {
                ctrl.BindDatatoGridFind(BaseDAO.ExecuteQuery(sql));
                Where = "where";
                hasAnd = false;
            }

            Cursor.Current = Cursors.Default;
            ctrl.Focus();
            ctrl.SelectRow();
        }

        private void frm_MachineWTSFind_Load(object sender, EventArgs e)
        {
            //list machine
            DataTable dt3;
            dt3 = BaseDAO.ExecuteQuery("select * from [222_Machine] order by MachineID asc");
            DataRow r3 = dt3.NewRow();
            r3["MachineID"] = "";
            dt3.Rows.InsertAt(r3, 0);
            cbMachine.DataSource = dt3;
            cbMachine.DisplayMember = "MachineID";
            cbMachine.ValueMember = "MachineID";

            //list MO Number
            //DataTable dt2;
            dtMOno = clsWTSDAO.GetAllMONo();
            DataRow r1 = dtMOno.NewRow();
            r1["MONo"] = "";
            r1["PartID"] = "";
            r1["FullName"] = "";
            dtMOno.Rows.InsertAt(r1, 0);
            cbMONo.DisplayMember = "MONo";
            cbMONo.ValueMember = "MONo";



            //list staff
            //DataTable dt4;
            dtStaff = clsWTSDAO.GetAllStaff();
            DataRow r4 = dtStaff.NewRow();
            r4["FullName"] = "";
            r4["StaffID"] = "";
            dtStaff.Rows.InsertAt(r4, 0);
            cbStaff.DataSource = dtStaff;
            cbStaff.DisplayMember = "StaffID";
            cbStaff.ValueMember = "StaffID";

            //list work
            DataTable dt1 = new DataTable();
            dt1 = clsWTSDAO.GetAllWorkMWTS();
            DataRow r2 = dt1.NewRow();
            r2["WorkID"] = "";
            dt1.Rows.InsertAt(r2, 0);
            cbWorkID.DataSource = dt1;
            cbWorkID.DisplayMember = "WorkID";
            cbWorkID.ValueMember = "WorkID";

            flagLoadDone = true;
        }

        private void cbMONo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flagLoadDone)
            {
                //list  OptionID
                DataTable dtOption = new DataTable();
                dtOption = clsWTSDAO.GetAllOptionID(cbMONo.SelectedValue.ToString());
                DataRow r5 = dtOption.NewRow();
                r5["OptionID"] = "";
                dtOption.Rows.InsertAt(r5, 0);
                cbOption.DataSource = dtOption;
                cbOption.DisplayMember = "OptionID";
                cbOption.ValueMember = "OptionID";
            }

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // Add controls for which Arrow keys => focus is not required.
            if ((!dropDown && (ActiveControl is TextBox || ActiveControl is System.Windows.Forms.ComboBox || ActiveControl is System.Windows.Forms.DateTimePicker)))
            {
                switch (keyData)
                {
                    case Keys.Up:
                        //case Keys.Down:

                        //bool forward = (keyData == Keys.Down);
                        if (this.ProcessTabKey(false))
                        {
                            return true;
                        }

                        break;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void cbShift_DropDown(object sender, EventArgs e)
        {
            dropDown = true;

        }

        private void cbShift_DropDownClosed(object sender, EventArgs e)
        {
            dropDown = false;

        }

        private void frm_LaborWTSFind_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.S)
                {
                    this.btnSearch.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btnSearch.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                dateDateFrom.Focus();
            }
        }

    }
}